package org.example.backend.controller;

import org.example.backend.service.AIAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/ai/policy")
@CrossOrigin(origins = "*")
public class AIAnalysisController {

    @Autowired
    private AIAnalysisService aiAnalysisService;

    /**
     * 智能解读（通过政策ID，自动查库并AI解读）
     * GET /api/ai/policy/interpretById?id=1
     */
    @GetMapping("/interpretById")
    public Map<String, Object> interpretById(@RequestParam String id) {
        return aiAnalysisService.interpretPolicyById(id);
    }

    /**
     * 智能解读（前端直接传政策内容）
     * POST /api/ai/policy/interpret
     * body: { "title": "...", "content": "..." }
     */
    @PostMapping("/interpret")
    public Map<String, Object> interpret(@RequestBody Map<String, Object> policy) {
        return aiAnalysisService.interpretPolicy(policy);
    }

    /**
     * 智能问答
     * POST /api/ai/policy/qa
     * body: { "question": "..." }
     */
    @PostMapping("/qa")
    public Map<String, Object> aiQA(@RequestBody Map<String, Object> question) {
        return aiAnalysisService.aiQA(question);
    }

    /**
     * 多政策对比分析
     * POST /api/ai/policy/compare
     * body: { "policies": [ ... ], "lang": "zh" }
     */
    @PostMapping("/compare")
    public Map<String, Object> comparePolicies(@RequestBody Map<String, Object> body) {
        try {
            System.out.println("收到政策对比分析请求: " + body);
            Map<String, Object> result = aiAnalysisService.comparePolicies(body);
            System.out.println("政策对比分析完成");
            return result;
        } catch (Exception e) {
            System.err.println("政策对比分析失败: " + e.getMessage());
            e.printStackTrace();
            return Map.of("success", false, "error", "政策对比分析失败: " + e.getMessage());
        }
    }

    /**
     * 分词（词云）
     * POST /api/ai/policy/segment
     * body: { "text": "..." }
     */
    @PostMapping("/segment")
    public Map<String, Object> segmentText(@RequestBody Map<String, Object> body) {
        return aiAnalysisService.segmentText(body);
    }

    /**
     * AI脑图生成
     * POST /api/ai/policy/mindmap
     * body: { "text": "..." }
     */
    @PostMapping("/mindmap")
    public Map<String, Object> generateMindmap(@RequestBody Map<String, Object> body) {
        return aiAnalysisService.generateMindmap(body);
    }

    /**
     * AI流程图生成
     * POST /api/ai/policy/flowchart
     * body: { "text": "..." }
     */
    @PostMapping("/flowchart")
    public Map<String, Object> generateFlowchart(@RequestBody Map<String, Object> body) {
        return aiAnalysisService.generateFlowchart(body);
    }

    /**
     * 生成政策对比图表数据
     * POST /api/ai/policy/chart-data
     * body: { "policies": [...], "chartType": "impact|time" }
     */
    @PostMapping("/chart-data")
    public Map<String, Object> generateChartData(@RequestBody Map<String, Object> body) {
        try {
            System.out.println("收到图表数据生成请求: " + body);
            Map<String, Object> result = aiAnalysisService.generateChartData(body);
            System.out.println("图表数据生成完成");
            return result;
        } catch (Exception e) {
            System.err.println("图表数据生成失败: " + e.getMessage());
            e.printStackTrace();
            return Map.of("success", false, "error", "图表数据生成失败: " + e.getMessage());
        }
    }

    /**
     * 导出政策对比结果
     * POST /api/ai/policy/export
     * body: { "format": "pdf|txt", "content": "...", "title": "..." }
     */
    @PostMapping("/export")
    public Map<String, Object> exportAnalysis(@RequestBody Map<String, Object> body) {
        try {
            System.out.println("收到导出请求: " + body);
            Map<String, Object> result = aiAnalysisService.exportAnalysis(body);
            System.out.println("导出完成");
            return result;
        } catch (Exception e) {
            System.err.println("导出失败: " + e.getMessage());
            e.printStackTrace();
            return Map.of("success", false, "error", "导出失败: " + e.getMessage());
        }
    }

    @ExceptionHandler(Exception.class)
    public Map<String, Object> handleException(Exception e) {
        return Map.of("success", false, "error", "AI JSON解析失败", "exception", e.getMessage());
    }
}
